package com.ruoyi.model.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.model.domain.DataUploadLog;
import com.ruoyi.model.domain.SysFileInfo;
import com.ruoyi.model.domain.TableBatchUploadLog;
import org.springframework.scheduling.annotation.Async;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.model.domain.TableInfo;
import com.ruoyi.model.service.ITableInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 基本信息Controller
 * 
 * @author genghz
 * @date 2021-10-20
 */
@RestController
@RequestMapping("/db/table")
public class TableInfoController extends BaseController
{
    @Autowired
    private ITableInfoService tableInfoService;

    /**
     * 查询基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('db:table:list')")
    @GetMapping("/list")
    public TableDataInfo list(TableInfo tableInfo)
    {
        startPage();
        List<TableInfo> list = tableInfoService.selectTableInfoList(tableInfo);
        return getDataTable(list);
    }

    @GetMapping("/tableOptions")
    public AjaxResult selectTableOptions(TableInfo tableInfo)
    {
        List<SysDictData> list = tableInfoService.selectTableOptions(tableInfo);
        return AjaxResult.success(list);
    }
    /**
     * 导出基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('db:table:export')")
    @Log(title = "基本信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TableInfo tableInfo)
    {
        List<TableInfo> list = tableInfoService.selectTableInfoList(tableInfo);
        ExcelUtil<TableInfo> util = new ExcelUtil<TableInfo>(TableInfo.class);
        return util.exportExcel(list, "基本信息数据");
    }

    /**
     * 获取基本信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('db:table:query')")
    @GetMapping(value = "/{tableId}")
    public AjaxResult getInfo(@PathVariable("tableId") Long tableId)
    {
        return AjaxResult.success(tableInfoService.selectTableInfoByTableId(tableId));
    }

    /**
     * 新增基本信息
     */
    @PreAuthorize("@ss.hasPermi('db:table:add')")
    @Log(title = "基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TableInfo tableInfo)
    {
        return toAjax(tableInfoService.insertTableInfo(tableInfo));
    }

    /**
     * 修改基本信息
     */
    @PreAuthorize("@ss.hasPermi('db:table:edit')")
    @Log(title = "基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TableInfo tableInfo)
    {
        return toAjax(tableInfoService.updateTableInfo(tableInfo));
    }

    /**
     * 删除基本信息
     */
    @PreAuthorize("@ss.hasPermi('db:table:remove')")
    @Log(title = "基本信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{tableIds}")
    public AjaxResult remove(@PathVariable Long[] tableIds)
    {
        return toAjax(tableInfoService.deleteTableInfoByTableIds(tableIds));
    }

    /**
     * 数据上传
     */
    @PreAuthorize("@ss.hasPermi('db:table:upload')")
    @Log(title = "数据上传", businessType = BusinessType.IMPORT)
    @PostMapping("/uploadData")
    @Async
    public AjaxResult uploadData(MultipartFile[] pdfFiles, Integer control, String[] fileNames, Long tableId )
    {
        tableInfoService.uploadData(pdfFiles, control, fileNames, tableId) ;
        return AjaxResult.success();
    }

    /**
     * 数据查看
     */
    @PreAuthorize("@ss.hasPermi('db:table:upload')")
    @Log(title = "数据查看", businessType = BusinessType.IMPORT)
    @GetMapping("/queryData")
    public AjaxResult queryData(@RequestParam("tableId") Long tableId,
                                @RequestParam(value="beginTime", required=false) String beginTime,
                                @RequestParam(value="endTime", required=false) String endTime,
                                @RequestParam(value="fileFlag", required=false) String fileFlag,
                                @RequestParam(value="currentPage", defaultValue = "1") Integer currentPage,
                                @RequestParam(value="pageSize", defaultValue = "10") Integer pageSize,
                                @RequestParam(value="keywords", defaultValue = "") String keywords)
    {
        Map<String, Object> maps = tableInfoService.queryData(tableId, currentPage, pageSize, beginTime, endTime, fileFlag, keywords);
        return AjaxResult.success(maps);
    }

    @Log(title = "数据删除", businessType = BusinessType.DELETE)
    @PostMapping("/removeRowEvent/{tableId}/{_rowId}")
    public AjaxResult removeRowEvent(@PathVariable("tableId") Long tableId,
                                     @PathVariable(value="_rowId") Long _rowId)
    {
        tableInfoService.removeRowEvent(tableId, _rowId) ;
        return AjaxResult.success();
    }

    @Log(title = "符件上传", businessType = BusinessType.IMPORT)
    @PostMapping("/uploadFileEvent")
    public AjaxResult uploadFileEvent(MultipartFile file,  Long tableId, Long _rowId) throws IOException
    {
        tableInfoService.uploadFileEvent(file, tableId, _rowId);
        return AjaxResult.success();
    }

    @GetMapping("/getTableBatchUploadLogInfo")
    public TableDataInfo getTableBatchUploadLogInfo(TableBatchUploadLog tableBatchUploadLog)
    {
        startPage();
        List<Map<Object, Object>> tableBatchUploadLogInfo = tableInfoService.getTableBatchUploadLogInfo(tableBatchUploadLog);
        return getDataTable(tableBatchUploadLogInfo);
    }

    @GetMapping("/getTableBatchUploadLogDetail/{batchId}")
    public TableDataInfo getTableUploadDetailLogByBatchId(@PathVariable("batchId") Long batchId) {
        List<DataUploadLog> tableUploadDetailLogByBatchId = tableInfoService.getTableUploadDetailLogByBatchId(batchId);
        return getDataTable(tableUploadDetailLogByBatchId);
    }

    @DeleteMapping("/removeLog/{logId}")
    public AjaxResult removeLog(@PathVariable("logId") Long logId) {
        tableInfoService.removeLog(logId);
        return AjaxResult.success();
    }

    @PostMapping("/restocking/{logId}")
    public AjaxResult restocking(@PathVariable("logId") Long logId) {
        tableInfoService.restocking(logId);
        return AjaxResult.success();
    }


    @GetMapping("/getFileInfo/{fileId}")
    public AjaxResult getFileInfo(Long fileId) throws IOException
    {
        SysFileInfo sysFileInfo = new SysFileInfo();
        sysFileInfo.setFileId(fileId);
        sysFileInfo.setFileName("中华人民共和国");
        sysFileInfo.setFilePath("中华人民共和国1");
        sysFileInfo.setContextType("中华人民共和国1");
        sysFileInfo.setSize(100101L);
        return AjaxResult.success(sysFileInfo);
    }
}
